//
//  BranddetailViewController.m
//  TextApplication
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "BranddetailViewController.h"
#import "TAMacro.h"
#import "TextView.h"
#import "ZWYNetTool.h"
#import "TableCellDetailCollectionViewCell.h"

#import "CollectionFirst.h"
#import "CollectionViewDataModel.h"
#import "CollectionViewItems.h"
#import "CollectionViewBrand.h"

#import "UIImageView+WebCache.h"
#import "LikeDetailViewController.h"

#import "MJRefresh.h"
#import "MBProgressHUD.h"

@interface BranddetailViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) CollectionFirst *first;

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIScrollView *backScrollView;

@property (nonatomic, strong) NSMutableArray <CollectionViewItems *>*dataArray;
@property (nonatomic, assign) NSInteger page;

@end

@implementation BranddetailViewController
//- (void)viewWillDisappear:(BOOL)animated {
//    self.navigationController.navigationBar.hidden = NO;
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.133 alpha:1.000];
//
//}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.094 alpha:1.000];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.page = 1;
    [self createBarButton];
    [self createTopView];
    [self createScrollView];
    [self createCollectionView];
    [self createContentView];

    [self createData];
}

- (void)createScrollView {
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 170, WIDTH, HEIGHT - 44 - 64 - 170)];
    self.backScrollView.scrollEnabled = NO; // 设置scrollView不能手势滑动 让其滚动
    self.backScrollView.backgroundColor =[UIColor colorWithWhite:0.184 alpha:1.000];
    self.backScrollView.pagingEnabled = YES;
    self.backScrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
    self.backScrollView.contentOffset = CGPointMake(WIDTH, 0);
    [self.view addSubview:self.backScrollView];
}


- (void)createContentView {
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, 40)];
    self.contentLabel.text = @"这条数据是空的";
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.lineBreakMode = UILineBreakModeWordWrap;
    CGSize size = [self.contentLabel sizeThatFits:CGSizeMake(self.contentLabel.frame.size.width, MAXFLOAT)];
    self.contentLabel.frame = CGRectMake(self.contentLabel.frame.origin.x, self.contentLabel.frame.origin.y, self.contentLabel.frame.size.width, size.height);
    self.contentLabel.backgroundColor = [UIColor colorWithWhite:0.184 alpha:1.000];
    self.contentLabel.textColor = [UIColor lightGrayColor];
    [self.backScrollView addSubview:self.contentLabel];
    
}

- (void)createTopView {
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 170)];
    self.topView.backgroundColor = [UIColor colorWithWhite:0.250 alpha:1.000];
    [self.view addSubview:self.topView];
    
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 2 - 30, 15, 60, 60)];
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.logo]];
    self.picImageView.layer.cornerRadius = 30;
    self.picImageView.layer.masksToBounds = YES;
    [self.topView addSubview:self.picImageView];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 95, WIDTH - 60, 40)];
    self.nameLabel.textAlignment = 1;
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.text = self.name;
    [self.topView addSubview:self.nameLabel];
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton.frame = CGRectMake(0, self.topView.bounds.size.height - 30, WIDTH / 2, 30);
    self.leftButton.backgroundColor = [UIColor colorWithRed:0.181 green:0.227 blue:0.270 alpha:1.000];
    self.leftButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.leftButton setTitle:@"品牌故事" forState:UIControlStateNormal];
    [self.leftButton addTarget:self action:@selector(storyClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.leftButton];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(WIDTH / 2, self.topView.bounds.size.height - 30, WIDTH / 2, 30);
    self.rightButton.backgroundColor = [UIColor whiteColor];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.rightButton setTitle:@"品牌产品" forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:@selector(brandClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.rightButton];
    
}

- (void)storyClick:(UIButton *)button {
    [UIView animateWithDuration:0.5 animations:^{
    self.backScrollView.contentOffset = CGPointMake(0, 0);
    }];
    
    [self.leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.leftButton.backgroundColor = [UIColor whiteColor];
    self.rightButton.backgroundColor = [UIColor colorWithRed:0.181 green:0.227 blue:0.270 alpha:1.000];
    [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

}

- (void)brandClick:(UIButton *)button {
    [UIView animateWithDuration:0.5 animations:^{
        self.backScrollView.contentOffset = CGPointMake(WIDTH, 0);
    }];
    self.leftButton.backgroundColor = [UIColor colorWithRed:0.181 green:0.227 blue:0.270 alpha:1.000];
    [self.leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.rightButton.backgroundColor = [UIColor whiteColor];
    [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}


- (void)createBarButton {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.173 alpha:1.000];
    UIButton *left = [TextView createButtonWithImage:[UIImage imageNamed:@"10"]];
    [left addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    
}

- (void)backClick:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}

- (void)createData {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");

    NSString *str1 = @"http://mobile.iliangcang.com/brand/brandShopList?a=b&page=";
    NSString *str3 = @"&brand_id=";
    NSString *str2 = @"&count=10&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
    NSString *urlStr = [[[[str1 stringByAppendingString:[NSString stringWithFormat:@"%ld", self.page]] stringByAppendingString:str3] stringByAppendingString:[NSString stringWithFormat:@"%@", self.dataID]] stringByAppendingString:str2];
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    [ZWYNetTool GET:urlStr andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        if (self.page == 1) {
            self.dataArray = [NSMutableArray array];
        }
        self.first = [CollectionFirst modelWithDic:result];
        for (CollectionViewItems *item in self.first.data.items) {
            [self.dataArray addObject:item];
        }
        if (self.dataArray.count != 0) {
            [hud hideAnimated:YES afterDelay:0.5];
        }
        //        NSLog(@"%@", self.first.data.items[0].goods_name);
        if (self.first.data.items.count != 0) {
            self.contentLabel.text = self.first.data.items[0].brand_info.brand_desc;
            self.page = self.page + 1;
            [self.collectionView reloadData];
        }
    } andFailureBlock:^(NSError *error) {
    }];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.collectionView.contentOffset.y < 0) {
        self.page = 1;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");
        NSLog(@"下拉刷新");
//        if (self.dataArray.count != 0) {
            [hud hideAnimated:YES afterDelay:1];
//        }
    }
}


// 上拉加载
- (void)upLoad {
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self createData];
        [self.collectionView.mj_footer endRefreshing];
    }];
}


- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30)/ 2, (WIDTH - 30)/ 2 + 100);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT - 64 - 44 - 170) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.184 alpha:1.000];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.backScrollView addSubview:self.collectionView];
    [self.collectionView registerClass:[TableCellDetailCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    [self upLoad];
    TableCellDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.collectData = self.dataArray[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LikeDetailViewController *detailVC = [[LikeDetailViewController alloc] init];
    detailVC.goodID = self.dataArray[indexPath.row].goods_id;
    [self.navigationController pushViewController:detailVC animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
